package gupao.concurrency.threads;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CountDownLatch;

@Slf4j
public class ThreadPriorityDemo {

    public static void main(String[] args) throws InterruptedException {
        for(int i = 0; i < 10; i++){
            System.out.println("======= Run " + i  + " : ");
            testRun();
        }
    }

    private static void testRun() throws InterruptedException {
        CountDownLatch latch = new CountDownLatch(5);

        Runnable r = ()-> {
            Utils.sleepIgnoreExp(10);
            log.info("{} run, priority: {}", Thread.currentThread().getName(), Thread.currentThread().getPriority());
            latch.countDown();
        };


        Thread t1 = new Thread(r,"t1");
        t1.setPriority(Thread.MIN_PRIORITY);

        Thread t3 = new Thread(r, "t3");
        t3.setPriority(3);

        Thread t5 = new Thread(r, "t5");
        t5.setPriority(Thread.NORM_PRIORITY);

        Thread t7 = new Thread(r, "t7");
        t7.setPriority(7);

        Thread t10 = new Thread(r, "t10");
        t10.setPriority(Thread.MAX_PRIORITY);

        t1.start();
        t3.start();
        t5.start();
        t7.start();
        t10.start();

        latch.await();
    }
}
